﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ViewUserInvoice : Page
{
    private List<_Article> document_article = new List<_Article>();

    private string Sort
    {
        get { return String.Concat(SortExpression, (SortDirection == SortDirection.Ascending) ? " ASC" : " DESC"); }
    }

    private SortDirection SortDirection
    {
        get
        {
            if (ViewState["SortDirection"] == null)
            {
                ViewState["SortDirection"] = SortDirection.Ascending;
            }

            return (SortDirection) ViewState["SortDirection"];
        }
        set { ViewState["SortDirection"] = value; }
    }

    private string SortExpression
    {
        get
        {
            if (ViewState["SortExpression"] == null)
            {
                ViewState["SortExpression"] = "ID";
            }

            return ViewState["SortExpression"].ToString();
        }
        set { ViewState["SortExpression"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.IsAuthenticated)
        {
            AuthenticatedMessagePanel.Visible = true;
            AnonymousMessagePanel.Visible = false;
            AuthenticatedMenuPanel.Visible = true;
            if (!IsPostBack)
            {
                SetData();
            }
        }
        else
        {
            AuthenticatedMessagePanel.Visible = false;
            AnonymousMessagePanel.Visible = true;
            AuthenticatedMenuPanel.Visible = false;
        }
    }

    public void SetData()
    {
        Response.Write(lbldocument_id.Text = Session["Doc_ID"].ToString());

        lbldoc_num.Text = _Invoice.ViewInvoice(lbldocument_id.Text).document_number;
        lbldoc_date.Text = _Invoice.ViewInvoice(lbldocument_id.Text).document_date.ToString("yyyy.MM.dd");
        lblpay_date.Text = _Invoice.ViewInvoice(lbldocument_id.Text).document_payment_date.ToString("yyyy.MM.dd");
        lblcompany_by.Text = _Invoice.ViewInvoice(lbldocument_id.Text).company_issued_by;
        lblcompany_to.Text = _Invoice.ViewInvoice(lbldocument_id.Text).client_issued_to;
        lbldoc_path.Text = _Invoice.ViewInvoice(lbldocument_id.Text).date_ceated.ToString("yyyy.MM.dd");
        lbldoc_type.Text = _Invoice.ViewInvoice(lbldocument_id.Text).document_type;
        lblpay_type.Text = _Invoice.ViewInvoice(lbldocument_id.Text).payment_type;

        Response.Clear();

        string document_id = lbldocument_id.Text;

        if (document_article.Count == 0)
        {
            lbl_art_message.Visible = true;
        }
        else
        {
            document_article = _Article.SlectArticle(document_id);
            article_list.DataSource = document_article;
            article_list.DataBind();
        }
    }

    protected void article_list_Sorting(object sender, ListViewSortEventArgs e)
    {
        if (SortExpression.Equals(e.SortExpression))
        {
            SortDirection = (SortDirection == SortDirection.Ascending)
                ? SortDirection.Descending
                : SortDirection.Ascending;
        }
        else
        {
            SortDirection = SortDirection.Ascending;
        }
        SortExpression = e.SortExpression;
        SetData();
    }
}